package com.jz.community.moduleshopping.goodsDetail.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.RxTimeTool;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.confirmOrder.bean.BaseOrderCouponInfo;
import com.ms.square.android.expandabletextview.ExpandableTextView;

import java.util.List;

public class GoodsCouponAdapter extends BaseQuickAdapter<BaseOrderCouponInfo, BaseViewHolder> {

    private String platformCouponId;

    public GoodsCouponAdapter(@Nullable List<BaseOrderCouponInfo> data) {
        super(R.layout.module_shopping_item_coupon_gt, data);
    }

    public GoodsCouponAdapter(@Nullable List<BaseOrderCouponInfo> data, String platformCouponId) {
        super(R.layout.module_shopping_item_coupon_gt, data);
        this.platformCouponId = platformCouponId;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    protected void convert(BaseViewHolder helper, BaseOrderCouponInfo entity) {

        TextView use_tv = helper.getView(R.id.coupon_gt_click_tv);//立即领取
        ImageView draw_iv = helper.getView(R.id.coupon_gt_draw_iv);//已领取

        LinearLayout top_ll = helper.getView(R.id.coupon_gt_top_ll);
        LinearLayout bot_ll = helper.getView(R.id.coupon_gt_bot_ll);

        top_ll.setBackgroundResource(R.drawable.border_coupon_red_top);
        bot_ll.setBackgroundResource(R.drawable.border_coupon_red_bot);

        TextView coupon_gt_all = helper.getView(R.id.coupon_gt_all);

        ExpandableTextView ex_tv = helper.getView(R.id.coupon_gt_expand);

        StringBuilder sb = new StringBuilder();

        if (entity.getCouponType() == 1) {
            //现金券
            helper.setText(R.id.coupon_gt_price, CharacterUtils.roundByScale(ConverterUtils.toDouble(entity.getCouponAmount())));
            helper.setText(R.id.coupon_gt_tv, "元");
            SHelper.vis(helper.getView(R.id.coupon_gt_tv));
        } else if (entity.getCouponType() == 2) {
            //折扣券
            double discountPrice = ConverterUtils.toDouble(entity.getCouponDiscount()) * 10;
            helper.setText(R.id.coupon_gt_price, CharacterUtils.roundByScale(discountPrice));
            SHelper.vis(helper.getView(R.id.coupon_gt_tv));
            helper.setText(R.id.coupon_gt_tv, "折");
            //免单券
        } else if (entity.getCouponType() == 3) {
            helper.setText(R.id.coupon_gt_price, CharacterUtils.roundByScale(ConverterUtils.toDouble(entity.getCouponAmount())));
            helper.setText(R.id.coupon_gt_tv, "元");
            SHelper.gone(helper.getView(R.id.coupon_gt_tv));
            coupon_gt_all.setText("免单卷");
            handleCouponItemInfo(entity, -1, sb, ex_tv, 3);
        }

        helper.setText(R.id.coupon_gt_rule, entity.getCouponName());


        if (entity.getMinFullAmount() > 0) {
            coupon_gt_all.setText("满¥" +  CharacterUtils.roundByScale(entity.getMinFullAmount()) + "使用");
        } else {
            coupon_gt_all.setText("无金额门槛");
        }

        helper.setText(R.id.coupon_gt_time, RxTimeTool.date2String(entity.getUseStartDate()) + "至" + RxTimeTool.date2String(entity.getUseEndDate()));


        switch (entity.getPlatformOrMerchant()) {

            case 0:
                //平台券适用范围
                if (entity.getCouponLimitShop() == 0) {
                    sb.append("全平台通用");
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 0);
                } else if (entity.getCouponLimitShop() == 1) {
                    sb.append("指定商家适用：");
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 0);
                } else if (entity.getCouponLimitShop() == 2) {
                    sb.append("指定类目适用：");
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 0);
                } else if (entity.getCouponLimitShop() == 3) {
                    sb.append("指定商品适用：");
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 0);
                }
                break;
            case 1:
                //商家卷适用范围
                if (entity.getCouponLimitGoods() == 0) {
                    if (!Preconditions.isNullOrEmpty(entity.getShopName())) {
                        sb.append("指定店铺适用：" + entity.getShopName());
                    }
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 1);
                } else if (entity.getCouponLimitGoods() == 1) {
                    if (!Preconditions.isNullOrEmpty(entity.getShopName())) {
                        sb.append("指定店铺适用：" + entity.getShopName());
                        sb.append("\n");
                    }
                    sb.append("指定商品适用：");
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 1);
                } else if (entity.getCouponLimitGoods() == 2) {
                    if (!Preconditions.isNullOrEmpty(entity.getShopName())) {
                        sb.append("指定店铺适用：" + entity.getShopName());
                        sb.append("\n");
                    }
                    sb.append("指定类目适用：");
                    handleCouponItemInfo(entity, entity.getPlatformOrMerchant(), sb, ex_tv, 1);
                }
                break;
        }

        if (entity.getHistoryStatus() == 1) {
            //已失效
            SHelper.gone(draw_iv);
            SHelper.vis(use_tv);
            helper.setText(R.id.coupon_gt_click_tv, "已失效");
            use_tv.setTextColor(R.color.white);
        } else if (entity.getHistoryStatus() == 2) {
            //已使用
            SHelper.gone(draw_iv);
            SHelper.vis(use_tv);
            helper.setText(R.id.coupon_gt_click_tv, "已使用");
            use_tv.setTextColor(R.color.white);
        } else if (entity.getHistoryStatus() == 3) {
            //已领取未使用
            SHelper.vis(draw_iv);
            SHelper.gone(use_tv);
        } else if (entity.getHistoryStatus() == 4) {
            //未领取
            SHelper.gone(draw_iv);
            SHelper.vis(use_tv);
            helper.setText(R.id.coupon_gt_click_tv, "立即领取");
        } else if (entity.getHistoryStatus() == 5) {
            //已领取未使用
            SHelper.gone(draw_iv);
            SHelper.vis(use_tv);
            helper.setText(R.id.coupon_gt_click_tv, "未启用");
            use_tv.setTextColor(R.color.white);
        }
        setCouponBackground(entity, top_ll, bot_ll);
        SHelper.vis(bot_ll);
        //点击事件
        helper.addOnClickListener(R.id.coupon_gt_click_tv);
        helper.addOnClickListener(R.id.coupon_gt_bot_ll);

        handelSelectedCoupon(helper, entity);

    }

    /**
     * 订单使用优惠券是否选中
     *
     * @param helper
     * @param entity
     */
    private void handelSelectedCoupon(BaseViewHolder helper, BaseOrderCouponInfo entity) {

        //店铺
        if ("Y".equals(entity.getChanged())) {
            SHelper.gone(helper.getView(R.id.coupon_gt_click_tv));
            SHelper.vis(helper.getView(R.id.coupon_gt_draw_iv));
        }

        //平台
        if (!Preconditions.isNullOrEmpty(platformCouponId)) {
            if (platformCouponId.equals(entity.getId())) {
                SHelper.gone(helper.getView(R.id.coupon_gt_click_tv));
                SHelper.vis(helper.getView(R.id.coupon_gt_draw_iv));
            }
        }
    }


    /**
     * 显示不同平台卷、店铺卷
     *
     * @param entity
     * @param platformOrMerchant
     * @param sb
     * @param ex_tv
     */
    private void handleCouponItemInfo(BaseOrderCouponInfo entity,
                                      int platformOrMerchant, StringBuilder sb, ExpandableTextView ex_tv, int couponType) {
        if (platformOrMerchant == 0) {
            if (!Preconditions.isNullOrEmpty(entity.getCouponItemList())) {
                for (BaseOrderCouponInfo.CouponItemListBean couponItemListBean : entity.getCouponItemList()) {

                    if (!Preconditions.isNullOrEmpty(couponItemListBean.getGoodsName())) {
                        sb.append(couponItemListBean.getGoodsName());
                        sb.append(" ");
                    }
                    if (!Preconditions.isNullOrEmpty(couponItemListBean.getGoodsCategoriesName())) {
                        sb.append(couponItemListBean.getGoodsCategoriesName());
                        sb.append(" ");
                    }
                }
                sb.append("\n");
            }
        } else if (platformOrMerchant == 1) {
            if (!Preconditions.isNullOrEmpty(entity.getCouponItemList())) {
                for (BaseOrderCouponInfo.CouponItemListBean couponItemListBean : entity.getCouponItemList()) {

                    if (!Preconditions.isNullOrEmpty(couponItemListBean.getGoodsName())) {
                        sb.append(couponItemListBean.getGoodsName());
                        sb.append(" ");
                    }

                    if (!Preconditions.isNullOrEmpty(couponItemListBean.getGoodsCategoriesName())) {
                        sb.append(couponItemListBean.getGoodsCategoriesName());
                        sb.append(" ");
                    }
                }
                sb.append("\n");
            }
        } else if (couponType == 3) {
            if (!Preconditions.isNullOrEmpty(entity.getCouponItemList())) {
                for (BaseOrderCouponInfo.CouponItemListBean couponItemListBean : entity.getCouponItemList()) {

                    if (!Preconditions.isNullOrEmpty(couponItemListBean.getGoodsName())) {
                        sb.append(couponItemListBean.getGoodsName());
                        sb.append(" ");
                    }

                    if (!Preconditions.isNullOrEmpty(couponItemListBean.getGoodsCategoriesName())) {
                        sb.append(couponItemListBean.getGoodsCategoriesName());
                        sb.append(" ");
                    }
                }
                sb.append("\n");
            }
        }
        if (!Preconditions.isNullOrEmpty(sb.toString()) && !Preconditions.isNullOrEmpty(entity.getCouponItemList())) {
            ex_tv.setText(sb.substring(0, sb.length() - 1));
        }else {
            ex_tv.setText(sb.substring(0, sb.length()));
        }
    }

    private void setCouponBackground(BaseOrderCouponInfo entity, LinearLayout
            top_ll, LinearLayout bot_ll) {
        if (entity.getHistoryStatus() == 1) {
            top_ll.setBackgroundResource(R.drawable.border_coupon_gray_top);
            bot_ll.setBackgroundResource(R.drawable.border_coupon_gray_bot);
        } else if (entity.getHistoryStatus() == 2) {
            top_ll.setBackgroundResource(R.drawable.border_coupon_gray_top);
            bot_ll.setBackgroundResource(R.drawable.border_coupon_gray_bot);
        } else if (entity.getHistoryStatus() == 3) {
            top_ll.setBackgroundResource(R.drawable.border_coupon_red_top);
            bot_ll.setBackgroundResource(R.drawable.border_coupon_red_bot);
        } else if (entity.getHistoryStatus() == 4) {
            top_ll.setBackgroundResource(R.drawable.border_coupon_red_top);
            bot_ll.setBackgroundResource(R.drawable.border_coupon_red_bot);
        } else if (entity.getHistoryStatus() == 5) {
            top_ll.setBackgroundResource(R.drawable.border_coupon_gray_top);
            bot_ll.setBackgroundResource(R.drawable.border_coupon_gray_bot);
        }
    }
}
